<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>更新用户信息</title>
    <style>
        body {
            font-family: '微软雅黑', Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f4;
        }
        .update-container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 90%;
            max-width: 400px;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        label {
            margin-top: 10px;
        }
        input[type="text"],
        input[type="password"] {
            padding: 10px;
            margin-top: 5px;
            margin-bottom: 20px;
            border: 1px solid #ddd;
            border-radius: 4px;
            width: calc(100% - 22px); /* 减去内边距和边框宽度 */
        }
        .update-button {
            padding: 10px;
            border: none;
            border-radius: 5px;
            color: white;
            background-color: #28a745;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .update-button:hover {
            background-color: #1e7e34;
        }
    </style>
</head>
<body>
<div class="update-container">
    <h1>更新用户信息</h1>
    <form id="updateUserForm">
        <label for="username">新用户名:</label>
        <input type="text" id="username" name="username" required><br>

        <label for="password">新密码:</label>
        <input type="password" id="password" name="password" required><br>

        <button type="submit" class="update-button">更新</button>
    </form>
</div>

<script>
    document.getElementById('updateUserForm').addEventListener('submit', function(event) {
        event.preventDefault();
        var username = document.getElementById('username').value;
        var password = document.getElementById('password').value;
        var urlParams = new URLSearchParams(window.location.search);
        var userId = urlParams.get('id');

        var user = {
            username: username,
            password: password
        };

        fetch('/api/users/' + userId, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(user)
        })
            .then(response => {
                // 首先检查响应状态码
                if (!response.ok) {
                    // 如果响应状态码不是200-299，抛出一个错误
                    throw new Error('Failed to update user');
                }
                // 如果响应状态码是200-299，解析JSON数据
                return response.json();
            })
            .then(data => {
                console.log('Success:', data);
                // 检查JSON数据中是否有错误字段
                if (data.error) {
                    alert(data.error); // 显示后端返回的错误消息
                } else {
                    alert(data.message); // 显示后端返回的成功消息
                }
                window.history.back(); // 更新成功后返回上一页
            })
            .catch((error) => {
                console.error('Error:', error);
                alert('更新用户信息时发生错误：' + error.message);
            });
    });
</script>
</body>
</html>